package edu.zut.cs.software.cms.module.all.service.impl;

import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import edu.zut.cs.software.cms.base.service.impl.GenericManagerImpl;
import edu.zut.cs.software.cms.module.all.dao.AllDao;
import edu.zut.cs.software.cms.module.all.domain.All;
import edu.zut.cs.software.cms.module.all.service.AllManager;

/**
 * ManagerImpl for All
 * 
 * @author 42276
 *
 */

//@Transactional
@Service // (value = "allManager")
public class AllManagerImpl extends GenericManagerImpl<All, Long> implements AllManager {

	AllDao allDao;

	@Autowired
	public void setAllDao(AllDao allDao) {
		this.allDao = allDao;
		this.dao = this.allDao;
	}
//	public static SqlSession getSqlSession() throws IOException {
//		InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
//		SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//		return sqlSessionFactory.openSession();
//	}
//	@Override
//	public List<All> getList() {
//		return allDao.selectAlls();
//	}
	@Override
	public All getAllById(Long id) {
		return allDao.selectById(id);
	}
	@Override
	public boolean IfExist(String username) {
		//数据存在返回false
		//数据不存在返回true
		if((allDao.IfOneExist(username))>0) 
			return false;
		else
			return true;
	}
	@Override
	public void CreateOne(String username, String password, String identity) {
		All all=new All();
		all.setUsername(username);
		all.setPassword(password);
		all.setIdentity(identity);
		allDao.save(all);
	}
	@Override
	public List<All> getList() {
		return (List<All>)this.dao.findAll();
	}
	@Override
	public Optional<All> findByAllId(Long allId) {
		Optional<All> result=this.allDao.findById(allId);
		return result;
	}
	@Override
	public Boolean deleteById(Long allId) {
		Optional<All> all=this.dao.findById(allId);
		if(all.equals(Optional.empty())) {
			return false;
		}else {
			this.dao.delete(all.get());
			return true;
		}
		
	}
	

}
